/**
 * 
 */
/**
 * author: tanyiming
 * date : 2014-09-28
 * description: 输入框自动查找功能
 * version: 1.0
 */

function categoryAutoSearch(id,url,init,callback){
	$("#"+id).autocomplete({
        source: function( request, response ) {
        	if(init!=undefined&&typeof(eval(init)) == "function")
        		init();
        	
            $.ajax({
            	type : 'POST',
                url: url,
                dataType: "json",
                data:{
                    cateSearchitem: request.term
                },
                success: function( data ) {
                    response( $.map( data.categorys, function( item ) {
                        return {
                            categoryId:item.categoryId,
                            categoryName:item.categoryName,
                            unit:item.unit,
                            parentId:item.parentId,
                            orderPrice:item.publishPrice,
                        }
                    }));
                }
            });
        },
        minLength: 1,
        select: function( event, ui ) {
        	$("#"+id).val(ui.item.categoryName);
        	if(callback!=undefined&&typeof(eval(callback)) == "function")
        		callback(ui);
            return false;
            
        }
    }).autocomplete( "instance" )._renderItem = function( ul, item ) {
  		return $( "<li>" )
    		.append( "<a>" + item.categoryName + "</a>" )
    		.appendTo( ul );
	};
	
}